﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YBApproval.Model;
using YBApproval.IDAL;
using YBApproval.BLL;

namespace YBApproval.Pages
{
    public partial class RoleEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (YBPSession.Instance.CurrentUser == null)
            {
                Response.Redirect(@"~/login.aspx");
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            //在该事件发生前：

            //Page 对象会针对每个控件和页调用 EnsureChildControls。 

            //设置了 DataSourceID 属性的每个数据绑定控件会调用 DataBind 方法。有关更多信息，请参见下面的数据绑定控件的数据绑定事件。

            //页上的每个控件都会发生 PreRender 事件。使用该事件对页或其控件的内容进行最后更改。

            base.OnPreRender(e);

            if (!IsPostBack)
            {
                InitData();
            }
        }

        private void InitData()
        {
            gvRoles.DataSource = YBPSession.Instance.AllRolesList;
            if(YBPSession.Instance.AllRolesList == null)
                gvRoles.DataSource = new List<Role>();
            gvRoles.DataBind();
            txtDesc.Text = "";
            txtName.Text = "";
        }

        protected void imbAddRole_Click(object sender, ImageClickEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtName.Text))
            {
                Role newRole = new Role();
                newRole.Name = txtName.Text;
                newRole.Desc = txtDesc.Text;
                newRole.CreateDt = DateTime.Now;
                newRole.LastUpdatedDt = DateTime.Now;
                newRole.IsActive = true;
                newRole.LastUpdatedBy = YBPSession.Instance.CurrentUser.Name;

                 using (IRole RoleDAL = DALFactory.GetDAL<IRole>())
                 {
                     newRole.Id =RoleDAL.InsertNewRole(newRole);
                     if (newRole.Id >= 0)
                     {

                         if (YBPSession.Instance.AllRolesList == null)

                             YBPSession.Instance.AllRolesList = new List<Role>();

                         YBPSession.Instance.AllRolesList.Add(newRole);
                     }
                 }
                 InitData();
            }
        }


        protected void gvRoles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")//如果是删除按钮
            {
                int rId = int.Parse(e.CommandArgument.ToString());//获取组编号

                using (IRole RoleDAL = DALFactory.GetDAL<IRole>())
                {
                    int result = RoleDAL.DeleteRoleById(rId);//使用三层架构实现,调用业务逻辑层的方法
                    if (result > 0)
                        YBPSession.Instance.AllRolesList.Remove(YBPSession.Instance.AllRolesList.SingleOrDefault(c => c.Id == rId));

                    InitData();// 删除之后,要重新绑定数据
                }
            }
        }

        protected void gvRoles_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //string rId = YBPSession.Instance.AllRolesList[e.RowIndex].Id;//获取组编号

            //using (IRole RoleDAL = DALFactory.GetDAL<IRole>())
            //{
            //    int result = RoleDAL.DeleteRoleById(rId);//使用三层架构实现,调用业务逻辑层的方法
            //    if (result > 0)
            //    {
            //        YBPSession.Instance.AllRolesList.Remove(YBPSession.Instance.AllRolesList.SingleOrDefault(c => c.Id == rId));
            //        Response.Write("删除成功！");
            //    }
            //    else
            //        Response.Write("删除失败！");

            //    InitData();// 删除之后,要重新绑定数据
            //}
        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect(@"~/Default.aspx");
        }
    }
}